
'use strict';

/**
	Part of Zombie Navigator
	Copyright © 2015  Zombie Navigator Developers
	
	@file
	@license AGPL-3.0+
*/


/*
	Zombie script example (= unprivileged)
	Do NOT require () this script from the addon code.
	
	demo_ID: 'main'
	A basic example of a Zombie script
*/


const {zombie, console} = require('zombie/zombie');
const {openWindow, closeAll} = require('zombie/webbrowser');
const {run, delay} = require('zombie/async');


// What's availbale in a Zombie script?
console.log('JSON: ' + typeof JSON.stringify);
console.log('self: ' + typeof self);
console.log('dump: ' + typeof dump);
console.log('Date: ' + typeof Date);

zombie.print('foo bar\n');


try {
run(function* () {
	let w = yield openWindow('about:');
	
	console.log(w);
	console.log(w.uri);
	console.log('isPrivate:', w.isPrivate);
	
	yield delay(2);
	let w2 = yield openWindow('data:,foobar');
	
	yield delay(1);
	yield w.close();
	
	yield delay(2);
	console.log('reloading...');
	yield w2.reload();
	console.log('reloaded');
	
	try {
		yield w2.runScript(function () {
			document.invalidFunc();
		});
		
		console.log('script succeeded');
	} catch (e) {
		console.warn('script exception:', e + '');
	}
	
	let result = yield w2.runScript(function (... args) {
		dump(' [dumped!] \n');
		console.log(args);
		console.log('window:', window + '', typeof window, Object.getPrototypeOf(window));
		console.log('in window:', Object.getOwnPropertyNames(window));
		console.log('window.exportFunction:', typeof window.exportFunction);
		console.log('exportFunction:', typeof exportFunction);
		console.log('top.self.port:', typeof top.self.port);
		console.log('window.top.self.port:', typeof window.top.self.port);
		console.log('frames.port:', typeof frames.port);
		console.log('frames.postMessage:', typeof frames.postMessage);
		console.log('frames.self.postMessage:', typeof frames.self.postMessage);
		console.log('frames.on:', typeof frames.on);
		console.log('frames.self.on:', typeof frames.self.on);
		console.log('frames.self.port:', typeof frames.self.port);
		console.log('frames.window.self.port:', typeof frames.window.self.port);
		console.log('frames.parent.self.port:', typeof frames.parent.self.port);
		console.log('parent.self.port:', typeof parent.self.port);
		console.log('self:', typeof self);
		console.log('self.document:', typeof self.document);
		console.log('self.port:', typeof self.port);
		console.log('window.window:', typeof window.window);
		console.log('window.window.self:', typeof window.window.self);
		console.log('window.window.self.document:', typeof window.window.document);
		console.log('window.window.self.port:', typeof window.window.self.port);
		if (window.window.self.port) {
			// this should never happen
			console.log('window.window.self.port.on:', typeof window.window.self.port.on);
		}
		
		window.foo = 42;
		console.log('window.foo set to:', window.foo); // this has no effect on scripts on the page
		
		window.exportFunction = function () {};
		console.log('defined: window.exportFunction', typeof exportFunction, 'exportFunction' in window, typeof window.exportFunction);
		
		console.log('in window:', Object.getOwnPropertyNames(window));
		
		let length = document.documentElement.innerHTML.length;
		console.log('length:', length);
		
		return {length: length};
	}, zombie.applicationName, zombie.applicationVersion);
	console.log('script returned: length=', result.length);
	
	
	let w3 = yield openWindow('about:buildconfig');
	yield delay(2);
	
	yield w2.close();
	w3.uri = 'about:config';
	yield w3.waitForReload();
	console.log('Loaded:', w3.uri);
	
	yield delay(2);
	yield w3.close();
	console.log('closed');
})
.catch(function (e) {
	console.warn('Async error:', e);
	closeAll();
});
} catch (e) {
	console.warn('BUG?: Uncaught error:', e);
	closeAll();
}


// vim: ts=4 noet ai

